package org.adl.registry.website;

import java.io.IOException;
import javax.servlet.ServletContext;
import org.adl.registry.website.rim.RimProxy;

/**
 *
 * @author marksj
 */
public abstract class SearchRequestHandler extends RequestHandler
{
    private String mPage;
    private String mQuery;
    
    /**
     *
     */
    public SearchRequestHandler()
    {
        this.mPage = "0";
        this.mQuery = "";
    }
    
    /**
     *
     * @return
     * @param iRimUrl
     * @param iClientAccessKey
     * @param iRegistry
     * @param iQuery
     * @throws java.io.IOException
     */
    public String sendSearchRequest()
    throws IOException
    {
        String rimResponse;
        
        ServletContext context = super.getServletContext();
        
        RimProxy p = new RimProxy(super.getRimUrl());
        p.openConnection();
        
        p.writeParam("command","searchrequest");
        
        p.writeParam("cak", super.getClientAccessKey());
        p.writeParam("registry", super.getRegistry());

        boolean useRimResponseType = PortalUtil.getUseRimResponseTypeParameter(context);
        
        if (useRimResponseType)
        {
            String responseType = "2000.2.3/2.1.1";
            
            p.writeParam("responsetype", responseType);
        }

        p.writeParam("page", this.mPage);
        
        p.writeParam("textsearch", this.mQuery);

        rimResponse = p.writeResponseTo();
        
        p.close();
        
        return rimResponse;
    }
    
    /**
     * Gets the query <CODE>String</CODE> provided by the user.
     * @return Returns the user provided query <CODE>String</CODE>.
     */
    public String getQuery()
    {
        return this.mQuery;
    }
    
    /**
     * 
     * @param iQuery 
     */
    protected void setQuery(String iQuery)
    {
        if (iQuery == null)
        {
            iQuery = "";
        }
        
        iQuery = iQuery.trim();
        
        this.mQuery = iQuery;
    }

    /**
     * Gets the page <CODE>String</CODE> provided by the user.
     * @return Returns the user provided query <CODE>String</CODE>.
     */
    public String getPage()
    {
        return mPage;
    }

    /**
     * 
     * @param iQuery 
     */
    public void setPage(String iPage)
    {
        this.mPage = iPage;
    }
}
